<template>
  <div class="c-user__setting">
    <div class="c-user__setting__item">
      <div class="c-martin__label c-article__title">文章标题: </div>
      <martin-input class="c-martin__title" v-model="title" placeholder="请输入文章标题"/>
    </div>
    <div class="c-user__setting__item">
      <div class="c-martin__label">撰写文章: </div>
      <!-- <martin-editor v-model="content"
        :options="editorOption"
        class="c-user__editor"
        ref="myQuillEditor">
      </martin-editor> -->
      <mavon-editor v-model="content" ref="md"/>
    </div>
    <div class="c-user__setting__item c-user__article__top">
      <div class="c-martin__label">置顶: </div>
      <Radio v-model="isTop">置顶</Radio>
    </div>
    <div class="c-user__save">
      <martin-button type="primary" @click="saveIntroce">发布</martin-button>
    </div>
  </div>
</template>

<script>
import { quillEditor } from 'vue-quill-editor'

export default {
  name: 'MartinSetting',
  components: {
    'martin-editor': quillEditor
  },
  data() {
    return {
      isTop: false,
      title: '',
      content: '',
      editorOption: {
        placeholder: '请输入文章内容'
      }
    }
  },
  mounted() {
    if (this.$route.query.id) this.getDetail(this.$route.query.id)
  },
  methods: {
    // 获取文章内容
    getDetail(id) {
      this.$http.get('/article/detail/' + id).then(({data, status}) => {
        if (status === 200) {
          this.title = data.title
          this.content = data.content
        }
      }).catch(err => {
        console.log(err)
      })
    },
    saveIntroce() {
      if (this.content) {
        this.$http[this.$route.query.id ? 'put' : 'post']('/write/article', {
          content: this.$refs.md.d_render,
          title: this.title,
          id: this.$route.query.id,
          isTop: this.isTop
        }).then(({data, status}) => {
          if (status === 200) {
            this.$message.success('保存成功')
          }
        }).catch(err => {
          console.log(err)
        })
      } else return void this.$message.error('请填写文章内容')
    }
  },
  watch: {
    content(n) {
      this.$nextTick().then(() => {
        console.log(this.$refs.md.d_render)
      })
    }
  }
}
</script>

<style scoped>
.c-user__setting__item /deep/ .v-note-wrapper {
  max-width: 100%;
  height: 400px;
}
.c-martin__label {
  flex: none;
  width: 60px;
  text-align: right;
}
.c-user__article__top {
  margin-top: 80px;
}
.c-article__title {
  margin-top: 20px;
}
.c-martin__title {
  float: left;
  width: 1000px;
}
.c-user__setting__item {
  display: flex;
  float: left;
  width: 100%;
  margin-bottom: 30px;
}
.c-user__editor {
  float: left;
  width: 800px;
  height: 200px;
}
.c-user__save {
  margin-top: 150px;
  float: left;
  padding-left: 85px;
}
</style>
